package com.xtgg.history;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;
import com.xtgg.R;
import com.xtgg.manager.BaseActivity;
import com.xtgg.util.ToastUtil;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class ShareActivity extends BaseActivity implements OnClickListener
{
	UMSocialService mController;
	
	String appId = "wx5a7b6ac492d7e609";
	String appSecret = "d883afeb2173582ca46bb9a7d109d414";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_share);
		
		((TextView) findViewById(R.id.center)).setText("分享");
		((Button) findViewById(R.id.right)).setText("发送");
		((Button) findViewById(R.id.right)).setTextColor(Color
				.parseColor("#EE7C1C"));
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);

		if(HistorySumActivity.mScreenShot != null)
		{
			((ImageView)findViewById(R.id.img)).setImageBitmap(HistorySumActivity.mScreenShot);
		}
		
		mController = UMServiceFactory.getUMSocialService("com.umeng.share");
		((EditText)findViewById(R.id.content)).setText(getIntent().getStringExtra("content"));
	}
	
	public Bitmap convertViewToBitmap(View view) {
		 WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		 int width = wm.getDefaultDisplay().getWidth();
		 int height = wm.getDefaultDisplay().getHeight();
		 
		 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
		 view.draw(new Canvas(b));
		 return b;
	 }
	
	@Override 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    super.onActivityResult(requestCode, resultCode, data);
	    /**使用SSO授权必须添加如下代码 */
	    UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode) ;
	    if(ssoHandler != null){
	       ssoHandler.authorizeCallBack(requestCode, resultCode, data);
	    }
	}
	
	@Override
	public void onClick(View arg0) {
		String content = ((EditText)findViewById(R.id.content)).getText().toString();
		
		switch (arg0.getId()) {
		case R.id.left:
			finish();
			break;
			
		case R.id.right:
			if(getIntent().getExtras().getInt("type") == 0)
			{
				UMWXHandler wxHandler = new UMWXHandler(this,appId,appSecret);
				wxHandler.addToSocialSDK();
				
				WeiXinShareContent weixinContent = new WeiXinShareContent();
				weixinContent.setShareContent(content);
				weixinContent.setTitle("小兔乖乖");
				weixinContent.setTargetUrl("http://www.xiaotuguai.com/");
				if(HistorySumActivity.mScreenShot != null) weixinContent.setShareImage(new UMImage(this,HistorySumActivity.mScreenShot));
				mController.setShareMedia(weixinContent);
				mController.postShare(this, SHARE_MEDIA.WEIXIN, new SnsPostListener()
				{
					@Override
					public void onComplete(SHARE_MEDIA arg0, int eCode,
							SocializeEntity arg2) {
						
						if (eCode == 200) {
							ToastUtil.showToastWithAlertPic("分享成功");
							finish();
                        } else {
                            String eMsg = "";
                            if (eCode == -101) {
                                eMsg = "没有授权";
                            }
                            ToastUtil.showToastWithAlertPic("分享失败[" + eCode + "] " + eMsg);
                        }
					}

					@Override
					public void onStart() {
						
					}
				});
			}
			else if(getIntent().getExtras().getInt("type") == 1)
			{
				UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, "100424468","c7394704798a158208a74ab60104f0ba");
				qqSsoHandler.addToSocialSDK();  
				
				QQShareContent qqShareContent = new QQShareContent();
				qqShareContent.setShareContent(content);
				qqShareContent.setTitle("小兔乖乖");
				qqShareContent.setTargetUrl("http://www.xiaotuguai.com/");
				if(HistorySumActivity.mScreenShot != null) qqShareContent.setShareImage(new UMImage(this,HistorySumActivity.mScreenShot));
				mController.setShareMedia(qqShareContent);
				mController.postShare(this, SHARE_MEDIA.TENCENT, new SnsPostListener()
				{
					@Override
					public void onComplete(SHARE_MEDIA arg0, int eCode,
							SocializeEntity arg2) {
						
						if (eCode == 200) {
							ToastUtil.showToastWithAlertPic("分享成功");
							finish();
                        } else {
                            String eMsg = "";
                            if (eCode == -101) {
                                eMsg = "没有授权";
                            }
                            ToastUtil.showToastWithAlertPic("分享失败[" + eCode + "] " + eMsg);
                        }
					}

					@Override
					public void onStart() {
						
					}
				});
			}
			else if(getIntent().getExtras().getInt("type") == 2)
			{
				UMWXHandler wxCircleHandler = new UMWXHandler(this,appId,appSecret);
				wxCircleHandler.setToCircle(true);
				wxCircleHandler.addToSocialSDK();
				
				CircleShareContent circleMedia = new CircleShareContent();
				circleMedia.setShareContent(content);
				circleMedia.setTitle("小兔乖乖");
				circleMedia.setTargetUrl("http://www.xiaotuguai.com/");
				if(HistorySumActivity.mScreenShot != null) circleMedia.setShareImage(new UMImage(this,HistorySumActivity.mScreenShot));
				mController.setShareMedia(circleMedia);
				mController.postShare(this, SHARE_MEDIA.WEIXIN_CIRCLE, new SnsPostListener()
				{
					@Override
					public void onComplete(SHARE_MEDIA arg0, int eCode,
							SocializeEntity arg2) {
						
						if (eCode == 200) {
							ToastUtil.showToastWithAlertPic("分享成功");
							finish();
                        } else {
                            String eMsg = "";
                            if (eCode == -101) {
                                eMsg = "没有授权";
                            }
                            ToastUtil.showToastWithAlertPic("分享失败[" + eCode + "] " + eMsg);
                        }
					}

					@Override
					public void onStart() {
						
					}
				});
			}
			else if(getIntent().getExtras().getInt("type") == 3)
			{
				mController.getConfig().setSsoHandler(new SinaSsoHandler());
				
				SinaShareContent circleMedia = new SinaShareContent();
				circleMedia.setShareContent(content);
				circleMedia.setTitle("小兔乖乖");
				circleMedia.setTargetUrl("http://www.xiaotuguai.com/");
				if(HistorySumActivity.mScreenShot != null) circleMedia.setShareImage(new UMImage(this,HistorySumActivity.mScreenShot));
				mController.setShareMedia(circleMedia);
				mController.postShare(this, SHARE_MEDIA.SINA, new SnsPostListener()
				{
					@Override
					public void onComplete(SHARE_MEDIA arg0, int eCode,
							SocializeEntity arg2) {
						
						if (eCode == 200) {
							ToastUtil.showToastWithAlertPic("分享成功");
							finish();
                        } else {
                            String eMsg = "";
                            if (eCode == -101) {
                                eMsg = "没有授权";
                            }
                            ToastUtil.showToastWithAlertPic("分享失败[" + eCode + "] " + eMsg);
                        }
					}

					@Override
					public void onStart() {
						
					}
				});
			}
			break;
		}
	}
}
